package com.pioneer.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.system.domain.SysDatabase;
import com.pioneer.web.system.service.ISysDatabaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 数据库备份管理
 *
 * @Author wf
 * @Date 2023-03-28 13:33:15
 **/
@RestController
@RequestMapping("/system/database")
public class SysDatabaseController extends BaseController {

    @Resource
    private ISysDatabaseService databaseService;

    /**
     * 获取数据库备份列表
     *
     * @param sysDatabase 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:sysDatabase:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDatabase sysDatabase) {
        boolean isPage = startPage();
        List<SysDatabase> list = databaseService.list(Wrappers.query(sysDatabase));
        return isPage ? getDataTable(list) : AjaxResult.success(list);
    }

    /**
     * 根据数据库备份id获取详细信息
     *
     * @param sysDatabaseId 数据库备份id
     * @return 结果
     */
    @GetMapping(value = "/getDatabaseById/{sysDatabaseId}")
    public AjaxResult getInfo(@PathVariable Long sysDatabaseId) {
        return AjaxResult.success(databaseService.getById(sysDatabaseId));
    }

    /**
     * 新增数据库备份
     *
     * @param sysDatabase 数据库备份
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:sysDatabase:add')")
    @Log(title = "数据库备份", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDatabase sysDatabase) {
        sysDatabase.setCreateBy(getUsername());
        return toAjax(databaseService.add(sysDatabase));
    }

    /**
     * 修改数据库备份
     *
     * @param sysDatabase 数据库备份
     * @return 结果
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDatabase sysDatabase) {
        return toAjax(databaseService.updateById(sysDatabase));
    }

    /**
     * 删除数据库备份
     *
     * @param sysDatabaseIds 数据库备份id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:sysDatabase:remove')")
    @Log(title = "数据库备份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sysDatabaseIds}")
    public AjaxResult remove(@PathVariable Long[] sysDatabaseIds) {
        return toAjax(databaseService.removeByIds(Arrays.asList(sysDatabaseIds)));
    }

    /**
     * 备份数据库
     *
     * @return 数据库文件
     */
    @GetMapping("/backupDatabase")
    @PreAuthorize("@ss.hasPerm('system:database:backupDatabase')")
    @Log(title = "数据库备份", businessType = BusinessType.BACKUP)
    public AjaxResult backupDatabase() {
        String fileName = databaseService.backupDatabase();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("fileName", fileName);
        return ajax;
    }

}
